#include "AdaptiveThreshold.h"
#include "../utils/Logger.h"
#include "Threshold.h"

AdaptiveThreshold::AdaptiveThreshold(
        int maxValue,
        int adaptiveMethod,
        int thresholdType,
        int aperture,
        int constant
) :
        maxValue(maxValue),
        adaptiveMethod(adaptiveMethod),
        thresholdType(thresholdType),
        aperture(aperture),
        constant(constant)
{
}

AdaptiveThreshold::~AdaptiveThreshold() {
}

IplImage* AdaptiveThreshold::apply(const IplImage* in) {
    Log << "Applying adaptive threshold effect: " <<
            maxValue << ", " <<
            adaptiveMethod << ", " <<
            thresholdType << ", " <<
            aperture << ", " <<
            constant << endl;

    bool releaseIn = false;
    if (in->nChannels != 1) {
        in = grayEffect.apply(in);
        releaseIn = true;
    }

    IplImage *out = cvCreateImage(cvSize(in->width, in->height), in->depth, in->nChannels);
    cvAdaptiveThreshold(in, out, maxValue, adaptiveMethod, thresholdType, aperture, constant);

    if (releaseIn)
        cvReleaseImage((IplImage**) &in);

    return out;
}
